/*
 
 * @Author: 舒克
 * @Official Accounts: 氧化1028
 * @email: 143587623@qq.com
 * @github: https://gitee.com/KingSio
 * @Date: 2021-01-26 19:16:14
 * @LastEditTime: 2021-03-17 18:17:50
 * @FilePath: \个人项目\vue\1.vue全站管理系统\vue_shop\src\store\modules\tagData.js
 */
import { ADDTAG, REMOVETAG } from '../mutation-type'
import Storage from '@/utils/Storage.js'
//面包屑
export default {
  state: {
    //如果sessionRoule这个为空，就用空数组
  tagList: Storage.sessionGetItem("sessionRoule")||[],//保存路由全部的数据

     //tagList:[],//保存路由全部的数据
    tagName: [],//["users", __ob__: Observer，切割后路由名称
    tagIndex: [],//保存路由地址
  },
  mutations: {
    //添加面包屑
    [ADDTAG](state, payload) {
      console.log(this.state.TagList.tagList)//与数据
      console.log(state, payload)
      let index =this.state.TagList.tagList.filter((value, i, arr) => {
        return value.path == payload.path //返回这个条件出去，如果数组已经存在这个路由对象，就不push
      })
      console.log(index)
      if (index.length == 0) {
        //如果添加不满足（value.path == payload.path？），会返回一个空数组,没有才push
        console.log('把路由数据push到定义的字段')
        this.state.TagList.tagList.push(payload);
        console.log(this.state.TagList.tagList)
        this.state.TagList.tagName.push(payload.cache);
        this.state.TagList.tagIndex.push(payload.path);
      }
      Storage.sessionSetItem("sessionRoule",this.state.TagList.tagList)   
      let index2=Storage.sessionGetItem("sessionRoule")
      console.log(index2);    
    },

    //删除面包屑
    [REMOVETAG](state, payload) {
      //state,当前的变量数据，payload是传过来的数据
      //首先获取会话的内容
      console.log(state, payload)
      let index = payload.index //获取当前路由的索引值
      let { router } = payload //路由对象
      let prePath = this.state.TagList.tagList[index].prePath;
      let preQuery = this.state.TagList.tagList[index].preQuery;
      //includes如果存在改字段就返回true,返之为false
      if (prePath && this.state.TagList.tagIndex.includes(prePath)) {
        router.push({
          path: prePath,
          query: preQuery
        })
      } else {
        //如果只剩下一个路由
        if (index > 0) {
          let pre = this.state.TagList.tagList[index - 1]
          router.push({
            path: pre,
            query: preQuery
          })
        }
      }
      /* 
      参数：index:当前删除的索引，1：删除项数
      */
      this.state.TagList.tagList.splice(index, 1)
      
      this.state.TagList.tagName.splice(index, 1)
      this.state.TagList.tagIndex.splice(index, 1)
      Storage.sessionSetItem("sessionRoule",this.state.TagList.tagList) 
      //这里需要重新刷新一下路由，暂时没找到解决办法
      if (prePath && this.state.TagList.tagIndex.includes(prePath)) {
        router.push({
          path: prePath,
          query: preQuery
        })
      } 
  
      /* this.state.tagList[index]
    当前的路由数据cache: undefined
    name: "用户管理"
    path: "/users"
    prePath: "/rights"
    preQuery: Object
    query: (...)
    */
    }
  }
}
